/**
 * 
 */
package ae.pcfc.cldos.online.web.controller.client.company.visitvisa.view;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import ae.pcfc.cldos.online.common.ConstantsCommon.Steps;
import ae.pcfc.cldos.online.common.UltimusConstants.AdminDecision;
import ae.pcfc.cldos.online.common.delegate.CompanyAdminDelegate;
import ae.pcfc.cldos.online.common.delegate.FnpGSDelegate;
import ae.pcfc.cldos.online.common.delegate.VisitVisaDelegate;
import ae.pcfc.cldos.online.common.exception.LeaseExpireException;
import ae.pcfc.cldos.online.common.exception.LicenseExpiredException;
import ae.pcfc.cldos.online.common.exception.PaymentException;
import ae.pcfc.cldos.online.common.vo.AdminDecisionVO;
import ae.pcfc.cldos.online.common.vo.CommentsVO;
import ae.pcfc.cldos.online.common.vo.FileVO;
import ae.pcfc.cldos.online.common.vo.UserVO;
import ae.pcfc.cldos.online.common.vo.VisitVisaMainVO;
import ae.pcfc.cldos.online.common.vo.employee.details.AddressDetailsVO;
import ae.pcfc.cldos.online.common.vo.employee.details.EmployeeDetailsVO;
import ae.pcfc.cldos.online.common.vo.employee.details.PassportDetailsVO;
import ae.pcfc.cldos.online.common.vo.employee.details.PersonalDetailsVO;
import ae.pcfc.cldos.online.common.vo.employee.details.VisitVisaVO;
import ae.pcfc.cldos.online.fnp.service.client.vo.FNPParameterVO;
import ae.pcfc.cldos.online.web.Constants;
import ae.pcfc.cldos.online.web.ViewPath;
import ae.pcfc.cldos.online.web.ViewUtils;
import ae.pcfc.cldos.online.web.controller.base.BaseNewVisaController;
import ae.pcfc.cldos.online.web.model.VisitVisaLicenseInfo;
import ae.pcfc.cldos.online.web.services.fnp.FnpParametersCheckService;

/**
 * @author Asna
 *
 */
@Controller
@RequestMapping(value = ViewPath.__COMPANY_VISIT_VISA_VIEW)
public class AdminVisitVisaViewController extends BaseNewVisaController{
	
	@Autowired
	@Qualifier("visitVisaDelegate")
	private VisitVisaDelegate visitVisaDelegate;
	
	@Autowired
	@Qualifier("companyAdminDelegate")
	private CompanyAdminDelegate companyAdminDelegate;
	
	private FnpGSDelegate FnpGSDelegate;
	@Resource(name = "FnpGSDelegate")
	public void setFnpGSDelegate(FnpGSDelegate FnpGSDelegate) {
		this.FnpGSDelegate = FnpGSDelegate;
	}
	
	@Autowired
	private FnpParametersCheckService fnpParametersCheckService;
	
	
	@ModelAttribute
	public void populateBreadCrumbs(ModelMap model, @RequestParam String appId) {
		breadCrumbsFactoryService.populateallAppReqDtlBreadCrumb(model);
	}
	
	@ModelAttribute(Constants.__COMPANY_VISIT_VISA_REGISTRATION_LICENSE_INFO_REQUEST_MODEL_KEY)
	public VisitVisaLicenseInfo populateRegisterEmploymentVisaReviewerInfoModel(@RequestParam String appId,
			HttpServletRequest request, ModelMap model) throws Exception {
		VisitVisaLicenseInfo visitVisaInfo = new VisitVisaLicenseInfo();
		
		VisitVisaMainVO visitVisaMainVO = visitVisaDelegate.getVisitVisaViewDtls(encryptionDelegate.decodeAndDecrypt(appId), getUserVO(), Steps.ALL_APP_REQUEST);
		
		AddressDetailsVO addressDetailsVO = visitVisaMainVO.getAddressDetailsVO();
		mapper.map(addressDetailsVO, visitVisaInfo);
		CommentsVO commentsVO = visitVisaMainVO.getCommentsVO();
		mapper.map(commentsVO, visitVisaInfo);
		EmployeeDetailsVO employeeDetailsVO = visitVisaMainVO.getEmployeeDetailsVO();
		mapper.map(employeeDetailsVO, visitVisaInfo);
		PassportDetailsVO passportDetailsVO = visitVisaMainVO.getPassportDetailsVO();
		mapper.map(passportDetailsVO, visitVisaInfo);
		PersonalDetailsVO personalDetailsVO = visitVisaMainVO.getPersonalDetailsVO();
		mapper.map(personalDetailsVO, visitVisaInfo);
		VisitVisaVO visitVisaVO = visitVisaMainVO.getVisitVisaVO();
		mapper.map(visitVisaVO, visitVisaInfo);
		visitVisaInfo.setWfStatus(visitVisaMainVO.getWfStatus());
		
		List<FileVO> fileVOList = visitVisaMainVO.getFileVOList();
		
		model.put("files", fileVOList);
		model.put("comments", visitVisaMainVO.getApplicationCommentVOs());
		
		return visitVisaInfo;
	}
	

	//added by asna end
	
	@RequestMapping(value = "/form.htm", method = RequestMethod.GET)
	public ModelAndView form() {
		return ViewUtils.DEFAULT_VIEW;
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/form.htm")
	public ModelAndView form(
			HttpServletRequest request,
			SessionStatus status,
			@RequestParam String appId,
			@RequestParam String fnpServiceCode,
			@RequestParam(required=false) String adminComments,
			@ModelAttribute(value = Constants.__COMPANY_VISIT_VISA_REGISTRATION_LICENSE_INFO_REQUEST_MODEL_KEY) @Valid VisitVisaLicenseInfo visitVisaInfo,
			BindingResult errors) throws Exception {
		
		System.out.println("review process submitted");
		UserVO userVO = getUserVO();
		AdminDecisionVO adminDecisionVO = new AdminDecisionVO();
		
		boolean isSubmitApproval = WebUtils.hasSubmitParameter(request, "MainUserApprove");
		boolean isSubmitReject = WebUtils.hasSubmitParameter(request, "MainUserReject");
		boolean isSubmitCancel = WebUtils.hasSubmitParameter(request, "MainUserCancel");
		
		if(isSubmitApproval){
			System.out.println("Main admin Approved...");
			adminDecisionVO.setStrActionType(AdminDecision.APPROVED.getAdminDecision());
		}else if(isSubmitReject){
			if(adminComments==null || adminComments.isEmpty()){
				errors.reject("admin.comment.required");
			}
			System.out.println("Main admin Rejected...");
			adminDecisionVO.setStrActionType(AdminDecision.REJECTED.getAdminDecision());
		} else if(isSubmitCancel){
			System.out.println("Main admin Canceled...");
			adminDecisionVO.setStrActionType(AdminDecision.CANCELLED.getAdminDecision());
		}
		if(!errors.hasErrors()){
			if(isSubmitApproval){
				List<FNPParameterVO> allParams = FnpGSDelegate.retreiveFNPParameters(Arrays.asList(new String[]{fnpServiceCode}));
				List<FNPParameterVO> notFeeled = fnpParametersCheckService.feelParamsAndGetNotFeeled(request, allParams);
				if(notFeeled.size()>0){
					errors.reject("sdgsd", "Recomplete payment info please");
				} else {
					adminDecisionVO.setFnpParams(allParams);
				}
			}
			adminDecisionVO.setApplicationId(encryptionDelegate.decodeAndDecrypt(appId));
			adminDecisionVO.setUserVO(userVO);
			adminDecisionVO.setFnpServiceCode(fnpServiceCode);
			adminDecisionVO.setComment(adminComments);
			
			try{
				companyAdminDelegate.companyAdminAction(adminDecisionVO);
			} catch (PaymentException e) {
				errors.reject("error.payment");
			} catch (LeaseExpireException e){
				errors.reject("lease.expire");
			} catch (LicenseExpiredException e) {
				errors.reject("license.expire");
			}
		}
		if(!errors.hasErrors()){
			return new ModelAndView(ViewPath.__REDIRECT+ViewPath.__ADMIN_TASKS+"/show.htm");
		} else {
			return ViewUtils.DEFAULT_VIEW;
		}
		
	}
	
}
